package renguanyu.app.codebuilder.view;

import javafx.collections.ObservableMap;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;

public class SaveTemplateHeadView extends AnchorPane {

    Label templateNameLabe = new Label("模板名称:");
    public TextField templateTextField = new TextField();
    Button addTemplateData = new Button("添加模板数据");

    SaveTemplateBodyView saveTemplateBodyView;
    ObservableMap<String, String> template;

    public ObservableMap<String, String> getTemplate() {
        return template;
    }

    public Label getTemplateNameLabe() {
        return templateNameLabe;
    }

    public TextField getTemplateTextField() {
        return templateTextField;
    }

    public SaveTemplateHeadView(SaveTemplateBodyView saveTemplateBodyView, ObservableMap<String, String> template) {
        this.saveTemplateBodyView = saveTemplateBodyView;
        this.template = template;
        initUI();
        initEvent();
        initData();
    }

    private void initData() {
        if(template!=null){
            templateTextField.setText(template.get("TEMP_NAME"));
        }

    }

    private void initUI() {

        AnchorPane.setLeftAnchor(templateNameLabe,15.0);
        AnchorPane.setTopAnchor(templateNameLabe,12.0);
        AnchorPane.setRightAnchor(templateNameLabe,400.0);

        AnchorPane.setLeftAnchor(templateTextField,100.0);
        AnchorPane.setTopAnchor(templateTextField,10.0);
        AnchorPane.setRightAnchor(templateTextField,110.0);

        AnchorPane.setTopAnchor(addTemplateData,10.0);
        AnchorPane.setRightAnchor(addTemplateData,10.0);

        this.getChildren().addAll(templateNameLabe,templateTextField,addTemplateData);

    }

    private void initEvent() {
        addTemplateData.setOnAction(event -> appendTemplateData());
    }


    private void appendTemplateData() {
        saveTemplateBodyView.appendOneRow();
    }

}
